part of base_package;

class PermissionUtil {
  static final PermissionUtil getInstance = PermissionUtil._internal();

  ///工厂构造函数
  factory PermissionUtil() => getInstance;

  ///构造函数私有化，防止被误创建
  PermissionUtil._internal();

  int EVENT_PATROL_PERMISSION = 3001;
  int EVENT_PERMISSION = 3002;

  /// 巡查权限请求dialog
  Future<bool> patrolPermissionDialog(BuildContext context) async {
    int locationAlwaysState =
        await isRequest(context, Permission.locationAlways);
    bool ignoringBatteryOptimizations =
        await KmxyBase.isIgnoringBatteryOptimizations();
    bool systemAlertWindow = await KmxyBase.isSystemAlertWindow();

    if (locationAlwaysState != 1 ||
        !ignoringBatteryOptimizations ||
        !systemAlertWindow) {
      await showDialog(
          barrierDismissible: true,
          useRootNavigator: false,
          context: context,
          builder: (BuildContext context) => PatrolPermissionDialog());
      return false;
    } else {
      return true;
    }
  }

  /// 权限请求dialog
  Future<bool> permissionDialog(BuildContext context) async {
    int phoneState = await isRequest(context, Permission.phone);
    int storageState = await isRequest(context, Permission.storage);
    int cameraState = await isRequest(context, Permission.camera);
    int micropState = await isRequest(context, Permission.microphone);
    int locationState = await isRequest(context, Permission.location);

    if (storageState != 1 ||
        phoneState != 1 ||
        cameraState != 1 ||
        micropState != 1 ||
        locationState != 1) {
      await showDialog(
          barrierDismissible: false,
          useRootNavigator: false,
          context: context,
          builder: (BuildContext context) => PermissionDialog());
      return false;
    } else {
      return true;
    }
  }

  /// 权限请求
  /// 传入对应权限
  ///     Permission.storage
  /// import 'package:permission_handler/permission_handler.dart';
  /// 权限列表详情查看 permission_android.txt 和 permission_ios.txt 文件
  /// return 0-未申请授权，1-同意授权，2-拒绝授权
  Future<int> isRequest(BuildContext context, Permission permission) async {
    PermissionStatus permissionStatus = await permission.status;
    if (permissionStatus == PermissionStatus.denied) {
      // 拒绝
      // request(context, permission);
      return 2;
    } else if (permissionStatus == PermissionStatus.granted) {
      // 允许
      return 1;
    } else if (permissionStatus == PermissionStatus.restricted) {
      // 拒绝ios
      // permissionSureDialog(context);
      return 2;
    } else if (permissionStatus == PermissionStatus.limited) {
      // 部分允许ios
      // permissionSureDialog(context);
      return 2;
    } else if (permissionStatus == PermissionStatus.permanentlyDenied) {
      // 永久拒绝android
      // permissionSureDialog(context);
      return 2;
    } else {
      return 0;
    }
  }

  /// 权限请求
  /// 传入对应权限
  ///     Permission.storage
  /// import 'package:permission_handler/permission_handler.dart';
  /// 权限列表详情查看 permission_android.txt 和 permission_ios.txt 文件
  request(BuildContext context, Permission permission, String hint) async {
    PermissionStatus permissionStatus = await permission.status;
    if (permissionStatus == PermissionStatus.denied) {
      // 拒绝
      PermissionStatus status = await permission.request();
      if (status == PermissionStatus.permanentlyDenied ||
          status == PermissionStatus.denied) {
        await permissionSureDialog(context, hint);
      }
    } else if (permissionStatus == PermissionStatus.granted) {
      // 允许
    } else if (permissionStatus == PermissionStatus.restricted) {
      // 拒绝ios
      await permissionSureDialog(context, hint);
    } else if (permissionStatus == PermissionStatus.limited) {
      // 部分允许ios
      await permissionSureDialog(context, hint);
    } else if (permissionStatus == PermissionStatus.permanentlyDenied) {
      // 永久拒绝android
      await permissionSureDialog(context, hint);
    } else {
      // 第一次
      await permission.request();
    }
  }

  permissionSureDialog(BuildContext context, String hint) async {
    await showDialog(
        barrierDismissible: false,
        context: context,
        builder: (BuildContext context) {
          return Platform.isIOS
              ? CupertinoAlertDialog(
                  title: const Text('提示'),
                  content: Column(
                    children: <Widget>[
                      const SizedBox(
                        height: 10,
                      ),
                      Align(
                        child: Text('“$hint”已被禁用，请跳转到APP设置中开启权限'),
                        alignment: const Alignment(0, 0),
                      ),
                    ],
                  ),
                  actions: <Widget>[
                    CupertinoDialogAction(
                      child: const Text('取消'),
                      onPressed: () {
                        Navigator.pop(context);
                      },
                    ),
                    CupertinoDialogAction(
                      child: const Text('跳转'),
                      onPressed: () {
                        Navigator.pop(context);
                        openAppSettings();
                      },
                    ),
                  ],
                )
              : AlertDialog(
                  title: const Text("提示"),
                  content: Text("“$hint”已被禁用，请跳转到APP设置中开启权限"),
                  actions: <Widget>[
                    TextButton(
                      child: const Text("取消"),
                      onPressed: () {
                        Navigator.pop(context);
                      },
                    ),
                    TextButton(
                      child: const Text("跳转"),
                      onPressed: () {
                        Navigator.pop(context);
                        openAppSettings();
                      },
                    ),
                  ],
                );
        });
  }
}
